package com.sgzn.sgpay.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by Chenkh on 2015/8/3.
 */
public class MoneyUtil {

    private static final char[] RMB_NUM = "零壹贰叁肆伍陆柒捌玖".toCharArray();
    private static final String[] UNITS_LOW = {"元", "角", "分", "整"};
    private static final String[] UNITS_MIDDLE = {"", "拾", "佰", "仟"};
    private static final String[] UNITS_HIGH = {"", "万", "亿"};

    /**
     * 元转分
     * @param amount 金额
     * @param decimalNum 保留小数
     * @return 单位为分的金额
     */
    public static BigDecimal yuanToCent(BigDecimal amount,int decimalNum){
        if(amount == null){
            return null;
        }
        amount = amount.multiply(BigDecimal.valueOf(100)).setScale(decimalNum,BigDecimal.ROUND_HALF_EVEN);
        return amount;
    }

    /**
     * 元转分
     * @param amount 金额
     * @param decimalNum 保留小数
     * @return 单位为分的金额
     */
    public static String yuanToCents(BigDecimal amount,int decimalNum){
        if(amount == null){
            return null;
        }
        amount = amount.multiply(BigDecimal.valueOf(100)).setScale(decimalNum,BigDecimal.ROUND_HALF_EVEN);
        return amount.toString();
    }


    /**
     * 将分转换为带2位小数的元，并格式化显示
     *
     * @param cent
     * @return
     */
    public static String cent2YuanWithFormat(String cent) {
        if (StringUtils.isEmpty(cent)) {
            return null;
        }
        BigDecimal amount = new BigDecimal(cent);
        amount = amount.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP); //转为带2位小数的元
        String money = String.valueOf(amount);
        String intSum = money.substring(0, money.indexOf('.'));
        String dot = money.substring(money.indexOf('.'), money.length());
        intSum = intSum.replaceAll("\\B(?=(?:\\d{3})+$)", ","); //格式化显示元
        return intSum + dot;
    }

    /**
     * 将分转换为带2位小数的元
     *
     * @param cent
     * @return
     */
    public static String cent2Yuan(String cent) {
        if (StringUtils.isEmpty(cent)) {
            return null;
        }
        BigDecimal amount = new BigDecimal(cent);
        DecimalFormat format = new DecimalFormat("###################0.00");
        return format.format(amount.divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
    }


    /**
     * 将分转换为中文大写，以元为单位
     *
     * @param cent
     * @return
     */
    public static String cent2Chn(String cent) {
        String integer;
        String fraction;
        int length = cent.length();
        if (length < 3) {
            //只有角分
            integer = "0";
            fraction = cent;
        } else {
            //最后2位为角分，其余为元
            integer = cent.substring(0, length - 2);
            fraction = cent.substring(length - 2);
        }

        String chnAmount = "";
        if (!"0".equals(integer)) {
            //元的部分
            chnAmount += integer2Rmb(integer) + UNITS_LOW[0];
        }
        if ("00".equals(fraction)) {
            //添加【整】
            chnAmount += UNITS_LOW[3];
        } else if (fraction.length() < 2 || (fraction.startsWith("0") && integer.equals("0"))) {
            //只有分的情况的下，去除前面所填充的【零】
            chnAmount += fraction2Rmb(fraction).substring(1);
        } else {
            chnAmount += fraction2Rmb(fraction);
        }
        return chnAmount;
    }

    /**
     * 角分部分转换为大写
     *
     * @param fraction
     * @return
     */
    private static String fraction2Rmb(String fraction) {
        char jiao = '0';
        char fen = '0';
        if (fraction.length() > 1) {
            jiao = fraction.charAt(0);
            fen = fraction.charAt(1);
        } else {
            fen = fraction.charAt(0);
        }

        return (RMB_NUM[jiao - '0'] + (jiao > '0' ? UNITS_LOW[1] : "")) +
                (fen > '0' ? RMB_NUM[fen - '0'] + UNITS_LOW[2] : "");
    }

    /**
     * 元部分转换为大写
     *
     * @param integer
     * @return
     */
    private static String integer2Rmb(String integer) {
        StringBuilder buffer = new StringBuilder();
        // 从个位数开始转换
        int i, j;
        for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
            char n = integer.charAt(i);
            if (n == '0') {
                // 当n是0且n的右边一位不是0时，插入[零]
                if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {
                    buffer.append(RMB_NUM[0]);
                }
                // 插入[万]或者[亿]
                if (j % 4 == 0 && (i > 0 && integer.charAt(i - 1) != '0'
                        || i > 1 && integer.charAt(i - 2) != '0'
                        || i > 2 && integer.charAt(i - 3) != '0')) {
                    buffer.append(UNITS_HIGH[j / 4]);
                }
            } else {
                if (j % 4 == 0) {
                    buffer.append(UNITS_HIGH[j / 4]);     // 插入[万]或者[亿]
                }
                buffer.append(UNITS_MIDDLE[j % 4]);         // 插入[拾]、[佰]或[仟]
                buffer.append(RMB_NUM[n - '0']); // 插入数字
            }
        }
        return buffer.reverse().toString();

    }

}
